﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.UI.WebControls;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections.Specialized;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [ToolboxData("<{0}:DateEditControl runat=server></{0}:DateEditControl>")]
    public class DateEditControl : EditControl
    {
        private TextBox _dateField;
        private HyperLink _linkCalendar;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            if (this.dateField == null)
            {
                this.dateField = new TextBox();
                this.dateField.ControlStyle.CopyFrom(this.ControlStyle);
                this.dateField.ID = this.ID + "date";
                this.Controls.Add(this.dateField);
            }
            this.Controls.Add(this.dateField);
            this.Controls.Add(new LiteralControl("&nbsp;"));
            if (this.linkCalendar == null)
            {
                this.linkCalendar = new HyperLink();
                this.linkCalendar.Text = "<img src=\"" + Globals.ApplicationPath + "/images/calendar.png\" border=\"0\" />";
            }
            this.linkCalendar.NavigateUrl = DotNetNuke.Common.Utilities.Calendar.InvokePopupCal(this.dateField);
            this.Controls.Add(this.linkCalendar);
        }

        protected virtual void LoadDateControls()
        {
            if (DateTime.Compare(this.DateValue, Null.NullDate) != 0)
            {
                this.dateField.Text = this.DateValue.Date.ToString("d");
            }
        }

        public override bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            this.EnsureChildControls();
            return base.LoadPostData(postDataKey + "date", postCollection);
        }

        protected override void OnDataChanged(EventArgs e)
        {
            PropertyEditorEventArgs args = new PropertyEditorEventArgs(this.Name) {
                Value = this.DateValue,
                OldValue = this.OldDateValue,
                StringValue = this.StringValue
            };
            base.OnValueChanged(args);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            this.LoadDateControls();
            if ((this.Page != null) & (this.EditMode == PropertyEditorMode.Edit))
            {
                this.Page.RegisterRequiresPostBack(this);
            }
        }

        protected override void RenderEditMode(HtmlTextWriter writer)
        {
            this.RenderChildren(writer);
        }

        protected override void RenderViewMode(HtmlTextWriter writer)
        {
            this.ControlStyle.AddAttributesToRender(writer);
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write(this.StringValue);
            writer.RenderEndTag();
        }

        public TextBox dateField
        {
            get
            {
                return this._dateField;
            }
            set
            {
                this._dateField = value;
            }
        }

        protected DateTime DateValue
        {
            get
            {
                DateTime nullDate = Null.NullDate;
                try
                {
                    nullDate = Conversions.ToDate(this.Value);
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    Exception exception = exception1;
                    ProjectData.ClearProjectError();
                }
                return nullDate;
            }
        }

        protected virtual string DefaultFormat
        {
            get
            {
                return "d";
            }
        }

        protected virtual string Format
        {
            get
            {
                string defaultFormat = this.DefaultFormat;
                if (this.CustomAttributes != null)
                {
                    foreach (Attribute attribute in this.CustomAttributes)
                    {
                        if (attribute is FormatAttribute)
                        {
                            FormatAttribute attribute2 = (FormatAttribute) attribute;
                            return attribute2.Format;
                        }
                    }
                }
                return defaultFormat;
            }
        }

        public HyperLink linkCalendar
        {
            get
            {
                return this._linkCalendar;
            }
            set
            {
                this._linkCalendar = value;
            }
        }

        protected DateTime OldDateValue
        {
            get
            {
                DateTime nullDate = Null.NullDate;
                try
                {
                    nullDate = Conversions.ToDate(this.OldValue);
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    Exception exception = exception1;
                    ProjectData.ClearProjectError();
                }
                return nullDate;
            }
        }

        protected override string StringValue
        {
            get
            {
                string nullString = Null.NullString;
                if ((DateTime.Compare(this.DateValue.ToUniversalTime().Date, DateTime.Parse("1754/01/01")) != 0) && (DateTime.Compare(this.DateValue, Null.NullDate) != 0))
                {
                    nullString = this.DateValue.ToString(this.Format);
                }
                return nullString;
            }
            set
            {
                this.Value = DateTime.Parse(value);
            }
        }
    }
}

